package com.zzyl.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.WxDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wx")
@Api(tags = "测试微信登录")
public class MyWxLoginController extends BaseController{

    private static  final Map<String,String> map = new HashMap<>();

    //url?key=value | url/value | JSON
    @PostMapping("/login")
    @ApiOperation("微信登录")
    public ResponseResult wxLogin(@RequestBody Map<String,String> map){
        //发送请求给微信平台获取微信用户的openid
        String openid = getOpenid(map.get("code"));
        System.out.println(openid);
        return success();
    }

    private String getOpenid(String code) {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("appid","wx43a0121a6f2a9d24");
        paramMap.put("secret","fe66c0d3c1b872cd78da935538efe398");
        paramMap.put("js_code", code);
        paramMap.put("grant_type","authorization_code");
        HttpResponse response = HttpUtil.createRequest(Method.GET, "https://api.weixin.qq.com/sns/jscode2session")
                .form(paramMap)
                .execute();
        String body = response.body();
        String openid = JSONUtil.parseObj(body).get("openid").toString();
        return openid;
    }

    @PostMapping("/getPhone")
    @ApiOperation("登录并获取手机号")
    public ResponseResult getPhone(@RequestBody WxDto dto){
        //1.获取access_token
        String accessToken = map.get("access_token");
        if(StrUtil.isEmpty(accessToken)){
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("grant_type","client_credential");
            paramMap.put("appid","wx43a0121a6f2a9d24");
            paramMap.put("secret","fe66c0d3c1b872cd78da935538efe398");
            HttpResponse response = HttpUtil.createRequest(Method.GET, "https://api.weixin.qq.com/cgi-bin/token")
                    .form(paramMap)
                    .execute();
            //json进行传递
            String body = response.body();
            accessToken = JSONUtil.parseObj(body).get("access_token").toString();
            map.put("access_token",accessToken);
        }
        //2.获取code
        String phoneCode = dto.getPhoneCode();
        //3.获取openid
        String openid = getOpenid(dto.getCode());
        //4.发送请求获取手机号
        Map<String,String> phoneMap = new HashMap<>();
        phoneMap.put("code",phoneCode);
        phoneMap.put("openid",openid);
        HttpResponse response = HttpUtil.createRequest(Method.POST, "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken)
                .body(JSONUtil.toJsonStr(phoneMap))
                .execute();
        String body = response.body();
        System.out.println(body);

        String phoneNum = JSONUtil.parseObj(body).getJSONObject("phone_info").get("phoneNumber").toString();
        System.out.println(phoneNum);


        return success();
    }

}
